package com.sy.service.impl;

import com.sy.dao.CollectDao;
import com.sy.dao.impl.CollectDaoImpl;
import com.sy.dto.Result;
import com.sy.entity.Collect;
import com.sy.service.CollectService;

import java.util.List;
import java.util.Objects;

import static com.sy.constant.BusinessConstant.CANCEL_COLLECT_SUCCESS_STATE;
import static com.sy.constant.BusinessConstant.COLLECT_SUCCESS_STATE;

public class CollectServiceImpl extends AbstractBaseServiceImpl implements CollectService {
    private CollectDao collectDao = new CollectDaoImpl();

    @Override
    public List<Integer> getCollectCarIdsByUserId(Integer userId) {
        return collectDao.getCollectCarIdsByUserId(userId);
    }

    @Override
    public int addOrCancelCollect(Integer userId, Integer carId) {
        throwIfNull(userId, "当前用户尚未登录");
        Collect collect = collectDao.getCollectInfo(userId, carId);
        if (Objects.isNull(collect)) {
            collectDao.addCollect(userId, carId);
            return COLLECT_SUCCESS_STATE;
        } else {
            collectDao.deleteCollect(userId, carId);
            return CANCEL_COLLECT_SUCCESS_STATE;
        }
    }
}
